package com.zyk.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.zyk.entity.Resource;
import com.zyk.entity.Role;
import com.zyk.mapper.ResourceMapper;
import com.zyk.service.IResourceService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.zyk.service.IRoleService;
import com.zyk.vo.FinalResult;
import com.zyk.vo.ResourceAllTreeVo;
import com.zyk.vo.ResourcetreeGridVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 资源 服务实现类
 * </p>
 *
 * @author feifeimao
 * @since 2017-11-20
 */
@Service
public class ResourceServiceImpl extends ServiceImpl<ResourceMapper, Resource> implements IResourceService {

    @Autowired
    private ResourceMapper resourceMapper;

    @Autowired
    private IRoleService roleService;



    /**
     * treeGrid
     * @return
     */
    public List<ResourcetreeGridVo> treeGird() {
        List<Resource> resourcesList = baseMapper.selectList(new EntityWrapper<Resource>(new Resource()));
        List<ResourcetreeGridVo> resourceVoList = new ArrayList<ResourcetreeGridVo>();
        for (Resource r : resourcesList) {
            ResourcetreeGridVo resourceVo = new ResourcetreeGridVo();
            BeanUtils.copyProperties(r, resourceVo);
            resourceVo.setIconCls(r.getIcon());
            resourceVoList.add(resourceVo);
        }
        return resourceVoList;
    }

    /**
     * allTree
     * @return
     */
    public Set<ResourceAllTreeVo> allTree() {
        List<Resource> resourceList = resourceMapper.allTree();
        Set<ResourceAllTreeVo> VoList = new HashSet<ResourceAllTreeVo>();
        return getResourceAllTreeVo(resourceList,VoList);
    }

    /**
     * allTrees
     * @return
     */
    public Set<ResourceAllTreeVo> allTrees() {
        List<Resource> resourceList = resourceMapper.allTrees();
        Set<ResourceAllTreeVo> VoList = new HashSet<ResourceAllTreeVo>();
        return getResourceAllTreeVo(resourceList,VoList);
    }

    /**
     * tree
     * @return
     */
    public Set<ResourceAllTreeVo> tree(Long uid) {
        List<Role> roleList = roleService.selectRoleIdListByUserId(uid);
        List<Long> roleIdList = new ArrayList();
        //存放role的角色名
        for (Role role : roleList) {
            roleIdList.add(role.getId());
        }
        //根据roleId获取所有的resouce
        List<Resource> resourceList = resourceMapper.tree(roleIdList);
        Set<ResourceAllTreeVo> VoList = new HashSet<ResourceAllTreeVo>();
        return getResourceAllTreeVo(resourceList,VoList);
    }

    /**
     * trees
     */
    public Set<ResourceAllTreeVo> trees(){
        List<Resource> resources = resourceMapper.selectList(new EntityWrapper<Resource>(new Resource()));
        Set<ResourceAllTreeVo> VoList = new HashSet<ResourceAllTreeVo>();
        Set<ResourceAllTreeVo> resourceAllTreeVo = getResourceAllTreeVo(resources, VoList);
        for (ResourceAllTreeVo vo:resourceAllTreeVo) {
            vo.setOpenMode(null);
        }
        return VoList;
    }


    /**
     * add
     * @return
     */
    public FinalResult add(Resource resource) {
        if(resourceMapper.add(resource)==1){
            return new FinalResult("添加成功",true);
        }
        return new FinalResult("添加失败",false);
    }

    /**
     * delete
     * @param resource
     * @return
     */
    public FinalResult delete(Resource resource) {
        if(super.deleteById(resource.getId())){
            return new FinalResult("删除成功",true);
        }
        return new FinalResult("删除失败",false);
    }

    /**
     * selectResourceListInRoleId
     * @return
     */
    public List<Resource> selectResourceListInRoleId(List<Long> roleId) {
        List<Resource> resources = baseMapper.selectResourceListInRoleId(roleId);
        return resources;
    }



    //重新封装resource为ResourceAllTreeVo对象
    public Set<ResourceAllTreeVo> getResourceAllTreeVo(List<Resource> list,Set<ResourceAllTreeVo> voList) {
        for (Resource resource:
             list) {
            ResourceAllTreeVo allterVo = new ResourceAllTreeVo();
            BeanUtils.copyProperties(resource, allterVo);
            allterVo.setText(resource.getName());
            if (resource.getOpened() == 1) {
                allterVo.setState("open");
            } else {
                allterVo.setState("close");
            }
            allterVo.setChecked(resource.getStatus()==1);
            allterVo.setOpenMode(resource.getOpenMode());
            allterVo.setAttributes(resource.getUrl());
            allterVo.setIconCls(resource.getIcon());
            voList.add(allterVo);
        }
        return voList;
    }
}
